//
//  MYInvestorListsViewController.m
//  投融保
//
//  Created by 523 on 13-12-18.
//  Copyright (c) 2013年 木叶科技. All rights reserved.
//

#import "MYInvestorListsViewController.h"
#import "InvestorCell.h"
#import "proCell.h"
#import "MYWebService.h"
#import "investPerson.h"
#import "TouZiPersonXQ.h"

#import "MYAgentCreated.h"

@interface MYInvestorListsViewController ()

@end

@implementation MYInvestorListsViewController

@synthesize investorTable;
extern NSString *key;
extern NSString *appKey;
NSString *userBaseId;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    SETBACKGROUND
   
    [self initView];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//初始话控件
- (void)initView
{
    int _Y=0;
    if (IOS_VERSION>=7.0)
        {_Y=20;}
    int _x=0;
    if (SCREEN_HEIGHT<=500) {
        _x=20;
    }
    UIView *titleView=[[UIView alloc] initWithFrame:CGRectMake(0, _Y, SCREEN_WIDTH, 104)];
    [titleView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"titile_bg"]]];
    [self.view addSubview:titleView];
    
    UILabel *lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(250, 2, 268, 100)];
    [lblTitle setText:@"投资人管理"];
    [lblTitle setTextColor:[UIColor whiteColor]];
    [lblTitle setFont:[UIFont boldSystemFontOfSize:40]];
    lblTitle.textAlignment=NSTextAlignmentCenter;
    lblTitle.backgroundColor=[UIColor clearColor];
    [titleView addSubview:lblTitle];
    
    
    UIButton *btnReturn=[UIButton new];
    [btnReturn setFrame:CGRectMake(0, 0, 105, 105)];
    [btnReturn setImage:[UIImage imageNamed:@"return_icon"] forState:UIControlStateNormal];
    [btnReturn.titleLabel setFont:[UIFont systemFontOfSize:22]];
    [btnReturn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnReturn addTarget:self action:@selector(btnReturnClick) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview: btnReturn];
    
    UIButton *btnCreatAccount=[UIButton new];
    [btnCreatAccount setFrame:CGRectMake(648, 2, 120, 100)];
    [btnCreatAccount setBackgroundImage:[UIImage imageNamed:@"btnRight1"] forState:UIControlStateNormal];
    [btnCreatAccount.titleLabel setFont:[UIFont systemFontOfSize:28]];
    [btnCreatAccount setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnCreatAccount setTitle:@"开户" forState:UIControlStateNormal];
    [btnCreatAccount addTarget:self action:@selector(btnCreateAccount) forControlEvents:UIControlEventTouchUpInside];
//    [titleView addSubview: btnCreatAccount];
    

    
    UIImageView *tableBgView=[UIImageView new];
    tableBgView.image=[UIImage imageNamed:@"bg"];
    self.investorTable =[UITableView new];
    [self.investorTable setFrame:CGRectMake(0, (_Y+104), SCREEN_WIDTH, SCREEN_HEIGHT-104-_Y)];
    [self.investorTable setDataSource:self];
    [self.investorTable setDelegate:self];
    [self.investorTable setBackgroundView:tableBgView];
    [self.investorTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self.view addSubview:self.investorTable];
    // ------异步加载数据
    if (personArray==nil) {
    
    mbHud = [[MBProgressHUD alloc] initWithView:self.view];
    mbHud.labelText = @"正在加载";
    [self.view addSubview:mbHud];
    mbHud.delegate = self;
    [mbHud show:YES];
    NSString *strAppInvestorRecordService=@"service=appInvestorRecordService&pageNo=1&pageSize=20";
    [[MYWebService new]getDataAsynWithKey:key encryptedString:strAppInvestorRecordService appKey:appKey];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getdata:)   name:@"getAsyData" object:nil];
    }
}

//返回按钮方法
- (void)btnReturnClick
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
//开户按钮
- (void)btnCreateAccount
{
    [self presentViewController:[MYAgentCreated new] animated:YES completion:nil];
//    [self presentModalViewController:[MYAgentCreated new] animated:YES];
}

-(void)getdata:(NSNotification *)asyData{
    if (asyData!=nil) {
        
    personArray = [NSMutableArray new];
    sleep(2);
        
    personArray = [[JsonParse new] inverstPersonPares:[asyData object]];
    
    [investorTable reloadData];
    [mbHud removeFromSuperview];
    mbHud = nil;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    }else{
        [mbHud removeFromSuperview];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    }
}
#pragma mark --------------TableView 协议方法-----------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return personArray.count;
//    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"reuseIdentifier";
    InvestorCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil) {
        NSArray *arr=[[NSBundle mainBundle] loadNibNamed:@"TouzirenCell" owner:self options:Nil];
        cell=[arr objectAtIndex:0];
    }
    
     [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell_bg" ]]];
//    cell.textLabel.text=[arrData objectAtIndex:indexPath.row];
    
    NSLog(@"%@",[[personArray objectAtIndex:indexPath.row] realName]);
    cell.lblIDnum.text = [[personArray objectAtIndex:indexPath.row] userName];
    if ([[[personArray objectAtIndex:indexPath.row] realName] isEqual:[NSNull null]]) {
        cell.lblName.text=@"暂无";
    }else
    {
        cell.lblName.text =[[personArray objectAtIndex:indexPath.row] realName];
    }
    
    cell.lblshouyi.text=[NSString stringWithFormat:@"%@ %%",[[personArray objectAtIndex:indexPath.row] distributionQuota]];

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  88;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 88;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //根据行点击得到数据 异步请求投资人详情
    userBaseId = [[personArray objectAtIndex:indexPath.row] userBaseId];
    [self presentViewController:[TouZiPersonXQ new] animated:NO completion:nil];
//    [self presentModalViewController:[TouZiPersonXQ new] animated:NO];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 760, 44)];
    [headerView setBackgroundColor:UIColorFromRGB(0xDFDFDF)];
    
    NSArray *arrText=[[NSArray alloc] initWithObjects:@"账户名",@"真实姓名",@"收益分配", nil];
    for (int i=0; i<=2; i++) {
        UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(214*i, 25, 212, 36)];
        lable.backgroundColor=[UIColor redColor];
        lable.text=[arrText objectAtIndex:i];
        lable.font=[UIFont systemFontOfSize:28];
        lable.textColor=UIColorFromRGB(0x357A8E);
        lable.backgroundColor=[UIColor clearColor];
        lable.textAlignment=NSTextAlignmentCenter;
        [headerView addSubview:lable];
    }

    return headerView;
}

#pragma mark-   -----------状态栏底色------
- (UIStatusBarStyle)preferredStatusBarStyle
{
    [self.view setBackgroundColor:[UIColor blackColor]];
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
@end
